package com.yonyou.crm.sys.role.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.model.util.IConverter;
import com.yonyou.crm.common.model.util.ModelUtil;
import com.yonyou.crm.sys.role.entity.FuncnodeDO;
import com.yonyou.crm.sys.role.entity.FuncpermissionDO;
import com.yonyou.crm.sys.role.entity.FuncpermissionVO;
import com.yonyou.crm.sys.role.entity.FuncresourceDO;
import com.yonyou.crm.sys.role.entity.FuncresourceVO;
import com.yonyou.crm.sys.role.entity.FunctionVO;
import com.yonyou.crm.sys.role.entity.RoleFuncPerDO;
import com.yonyou.crm.sys.role.entity.RoleFuncPerVO;
@Service
public class FunctionBizServiceImpl extends BaseBizServiceImpl<FuncpermissionDO>{
	
	public List<FunctionVO> getFunctionList(Long roleId){
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("roleId", roleId);
		List<FuncnodeDO> functionList = dao.selectVoList(FuncnodeDO.class, "getFunctionNodeList", paramMap);
		final List<RoleFuncPerDO> roleFuncPerList = dao.selectVoList(RoleFuncPerDO.class, "getRoleFuncPerData", paramMap);
		//DO转VO
		List<FunctionVO> functionVOs = ModelUtil.copyModelList(functionList,FunctionVO.class,new IConverter<FuncnodeDO,FunctionVO>(){
			@Override
			public void convert(FuncnodeDO origObj, FunctionVO destObj) {
				destObj.setChild(ModelUtil.copyModelList(origObj.getChild(),FuncpermissionVO.class,new IConverter<FuncpermissionDO,FuncpermissionVO>(){
					@Override
					public void convert(FuncpermissionDO funcperDO,
							FuncpermissionVO destObj) {
						destObj.setChecked("F");
						if(roleFuncPerList==null||roleFuncPerList.size()==0){
							return;
						}else{
							for(RoleFuncPerDO perDo:roleFuncPerList){
								if(perDo.getFuncperId()==funcperDO.getId()){
									destObj.setChecked("T");
									return;
								}
							}
						}
					}
				 }));
			}
		});

		return functionVOs;
	}

	public List<FuncresourceVO> getFuncResourceList(long roleId) {
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("roleId", roleId);
		List<FuncresourceDO> funcresourceDOs = dao.selectVoList(FuncresourceVO.class, "getFuncResourceList", paramMap);
		List<FuncresourceVO> roleFuncPerVOs = ModelUtil.copyModelList(funcresourceDOs, FuncresourceVO.class);

		return roleFuncPerVOs;
	}



	public List<FuncresourceVO> getUnhaveFuncResourceList(long roleId) {
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("roleId", roleId);
		final List<FuncresourceDO> funcresourceDOs = dao.selectVoList(FuncresourceVO.class, "getFuncResourceList", paramMap);
		List<FuncresourceDO> allFuncresourceDOs = dao.selectVoList(FuncresourceVO.class, "pageFuncresource", new HashMap());
//		List<FuncresourceVO> roleFuncPerVOs = ModelUtil.copyModelList(funcresourceDOs, FuncresourceVO.class);
		List<FuncresourceVO> roleFuncPerVOs = ModelUtil.copyModelList(allFuncresourceDOs,FuncresourceVO.class,new IConverter<FuncresourceDO,FuncresourceVO>(){
			@Override
			public void convert(FuncresourceDO funcresDO,
								FuncresourceVO destObj) {
				destObj.setIsDisplay(2);
				for(int i=0;i<funcresourceDOs.size();i++){
					if(funcresourceDOs.get(i).getId().equals(destObj.getId())){
						destObj.setIsDisplay(1);
					}
				}
			}
		});
		return roleFuncPerVOs;
	}

	public void assignFuncPer(List<RoleFuncPerVO> roleFuncPerVOs, String checked) {
		List<RoleFuncPerDO> roleFuncPerDOs = ModelUtil.copyModelList(roleFuncPerVOs, RoleFuncPerDO.class);
		if("T".equals(checked)){
			dao.saveSql(RoleFuncPerDO.class, "insertRoleFuncPers", roleFuncPerDOs);
		}else{
			dao.deleteSql(RoleFuncPerDO.class, "deleteRoleFuncPers", roleFuncPerDOs);
		}
	}
	
	public List<FuncresourceVO> getFuncResourceList(Map<String, Object> param) {
		List<FuncresourceDO> funcresourceDOs = dao.selectVoList(FuncresourceVO.class, "getFuncResourceList", param);
		List<FuncresourceVO> roleFuncPerVOs = ModelUtil.copyModelList(funcresourceDOs, FuncresourceVO.class);
		return roleFuncPerVOs;
	}

}
